package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.statistics.ReportDataDaySta;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.ProportionService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtils;
import com.tbit.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author HZW
 * @ClassName ProportionController
 * @Description 车辆运营报表数据统计，各种数据占比
 * @date 2022/2/22 17:12
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/proportion")
public class ProportionController extends BaseController {
    @Resource
    private ProportionService proportionService;

    public static void main(String[] args) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        System.out.println(todayStart.getTime().getTime());
    }

    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId, Long startTime, Long endTime, String lowBatteryTime, String lowEndBatteryTime) throws ParseException {
        Assert.notNull(accountId, "ver.accountId.notNull");
        Assert.notNull(startTime, "ver.startTime.notNull");
        Assert.notNull(endTime, "ver.endTime.notNull");
        Assert.notNull(lowBatteryTime, "低电时间不能为空");
        Assert.notNull(lowEndBatteryTime, "低电时间不能为空");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.format(new Date());
        if (startTime >= DateTimeUtil.getTodayTime()) {
            throw new BaseException("只能查看历史数据");
        }
        List<ReportDataDaySta> list = proportionService.queryPage(accountId, startTime, endTime, lowBatteryTime, lowEndBatteryTime);
        return JsonResult.ok(list, list.size());
    }

    @RequestMapping("/excel")
    public JsonResult excel(Integer accountId, Long startTime, Long endTime, String lowBatteryTime, HttpServletResponse response, String lowEndBatteryTime) throws ParseException {
        Assert.notNull(accountId, "ver.accountId.notNull");
        Assert.notNull(startTime, "ver.startTime.notNull");
        Assert.notNull(accountId, "ver.endTime.notNull");
        Assert.notNull(lowBatteryTime, "低电时间不能为空");
        Assert.notNull(lowEndBatteryTime, "低电时间不能为空");
        List<ReportDataDaySta> list = proportionService.queryPage(accountId, startTime, endTime, lowBatteryTime, lowEndBatteryTime);

        LinkedHashMap<String, String> Total = new LinkedHashMap<String, String>();

        Total.put("dataDateStr", "日期");
        Total.put("machineNum", "每日车辆周转率");
        Total.put("maintain", "每日维修完成率");
        Total.put("dispatchNum", "调度车辆有效率");
        Total.put("superzone", "挪车完成率");
        Total.put("lowVoltage", String.format("低电车辆占比(%s)", lowBatteryTime));
        Total.put("lowEndVoltage", String.format("低电车辆占比(%s)", lowEndBatteryTime));
        //Total.put("lowBatteryHalfPastSeven", "低电车辆占比(16:30)");
        //Total.put("lowBatteryHalfPastFourteen", "低电车辆占比");


        LinkedHashMap<String, String> titleMap = Total;
        try {
            ExcelUtils.listToExcel(list, titleMap, "运营报表", response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            if (list.isEmpty()) {
                throw new BaseException(SystemCodeEnum.NOT_DATA);
            }
        }
        return JsonResult.ok();
    }

}
